import random
import time
from DBUlist import select, inster

# print(time.strftime("%Y-%m-%d %H:%M:%S"))

bank_name = "中国工商银行"

print('*********************************')
print('*     中国工商银行               *')
print("*********************************")
print("|   1、开户                      |")
print("|   2、存钱                      |")
print("|   3、取钱                      |")
print("|   4、转账                      |")
print("|   5、查询                      |")
print("|   6、再见                      |")
print("*********************************")
bank = {}


def adduser():
    account = random.randint(10000000, 99999999)
    username = input("请输入您用户名:")
    password = int(input("请输入您的密码:"))
    country = input("\t\t请输入您的国家:")
    province = input("\t\t请输入您的省份:")
    street = input("\t\t请输入您的街道:")
    door = input("\t\t请输入您的门牌号:")
    a = addbank(account, username, password, country, province, street, door)
    if a == 1:
        print("开户成功,以下是您的详细信息")
        info = '''
                =========中国工商银行=======
                    1、用户名:%s
                    2、密码******
                    3、国家:%s
                    4、省份:%s
                    5、街道:%s
                    6、门派:%s
                    7、账户:%s
                    8、钱:%s
        '''
        sql = "select money  from user where username=%s"
        param = username
        date = int(select(sql, param, mode="one")[0])
        print(info % (username, country, province, street, door, account, date))
    elif a == 2:
        print("重名")
    elif a == 3:
        print("已满")


#
def repeater(name):
    sql = "select username  from user where username=%s"
    param = [name]

    data = len(select(sql, param))
    return data


def max():
    sql = "select username from user where money>=0"
    data = len(select(sql))

    return data


def addbank(account, username, password, country, province, street, door):
    if max() >= 1000000000:
        return 3

    elif repeater(username) != 0:
        return 2
    else:
        sql = "insert into user values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        param = [account, username, password, country, province, street, door, 0, time.strftime("%Y-%m-%d %H:%M:%S"),
                 bank_name]
        inster(sql, param)
    return 1


# 存钱
def login():
    sql_account = "SELECT account FROM user"
    num2 = select(sql_account, mode="all")
    print("现在已有的账户:", num2)
    sql_1 = "SELECT account FROM USER where account =%s"
    username_login = int(input("请输入账户:"))
    param = [username_login]
    data = len(select(sql_1, param))
    if data != 0:
        sql_money = "UPDATE USER SET money=money+%s  WHERE account =%s"
        money_login = int(input("请输入存钱的金钱:"))
        param = [money_login, username_login]
        inster(sql_money, param)
        sql_money1 = "SELECT money FROM USER WHERE account = %s"
        param = [username_login]
        num4 = select(sql_money1, param)
        print("您存的金额为:", money_login, "您的总金额为:", num4)
        return True
    else:
        print("该账户不存在")
        return False


# 取钱
def cancel_dic():
    sql_account = "SELECT account FROM user"
    num2 = select(sql_account, mode="all")
    print("现在已有的账户:", num2)
    sql_1 = "SELECT account FROM USER where account =%s"
    username_cancel = int(input("请输入您取钱的账户"))
    param = [username_cancel]
    data = len(select(sql_1, param))
    if data == 0:
        return 1
    else:
        sql_password = "SELECT account FROM USER WHERE PASSWORD =%s and account=%s"
        password_cancel = int(input("请输入您的密码："))
        param = [password_cancel, username_cancel]
        select(sql_password, param)
        password_num = len(select(sql_password, param))
        if password_num == 0:
            return 2
        else:

            money_cancel = int(input("请输入取钱的金额："))
            sql_money3 = "SELECT money FROM USER WHERE account=%s AND money>=%s"
            param = [username_cancel, money_cancel]
            select(sql_money3, param)
            money_q = len(select(sql_money3, param))
            if money_q == 0:
                return 3
            else:
                sql_money = "UPDATE USER SET money = money -%s WHERE account = %s"
                param = [money_cancel, username_cancel]
                inster(sql_money, param)
                sql_money1 = "SELECT money FROM USER WHERE account=%s"
                param = [username_cancel]
                num_w = (select(sql_money1, param))
                print("剩余的金额为:", num_w)
                return 0


def cancel():
    name_cancel = cancel_dic()
    if name_cancel == 1:
        print("输入的用户名不存在")
    elif name_cancel == 2:
        print("输入的密码不正确")
    elif name_cancel == 3:
        print("当前余额不足")
    elif name_cancel == 0:
        print("取钱成功")


def turn_money_num():
    turn_money_1 = turn_money()
    if turn_money_1 == 1:
        print("转出或转入的账户不存在")
    elif turn_money_1 == 2:
        print("转出账户的密码不正确")
    elif turn_money_1 == 3:
        print("转出账户余额不足")
    elif turn_money_1 == 0:
        print("转账成功")


def turn_money():
    turn_money_dic1 = 0  # 转入的金额
    sql_account = "SELECT account FROM user"
    num2 = select(sql_account, mode="all")
    print("现在已有的账户:", num2)
    turn_username = int(input("请输入转出账户："))
    # 转出
    sql_1 = "SELECT account FROM USER where account =%s"
    param = [turn_username]
    data = len(select(sql_1, param))
    turn_username1 = int(input("请输入转入账户："))
    sql_2 = "SELECT account FROM USER where account =%s"
    param = [turn_username1]
    data1 = len(select(sql_2, param))
    if data == 0 or data1 == 0:
        return 1
    else:
        turn_password = int(input("请输入转出的账户密码："))
        sql_password = "SELECT account FROM USER WHERE PASSWORD =%s and account=%s"
        param = [turn_password, turn_username]
        select(sql_password, param)
        password_num = len(select(sql_password, param))
        if password_num == 0:
            return 2
        else:
            turn_money_dic = int(input("请输入转出的金额："))
            sql_money3 = "SELECT money FROM USER WHERE account=%s AND money>=%s"
            param = [turn_username, turn_money_dic]
            select(sql_money3, param)
            money_q = len(select(sql_money3, param))
            if money_q == 0:
                return 3
            else:
                sql_money = "UPDATE USER SET money = money -%s WHERE account = %s"
                param = [turn_money_dic, turn_username]
                inster(sql_money, param)
                sql_money1 = "SELECT money FROM USER WHERE account=%s"
                param = [turn_username]
                num_w = (select(sql_money1, param))
                print("您的余额还剩", num_w)

                sql_money = "UPDATE USER SET money = money + %s WHERE account = %s"
                param = [turn_money_dic, turn_username1]
                inster(sql_money, param)
                sql_money1 = "SELECT money FROM USER WHERE account=%s"
                param = [turn_username1]
                num_w1 = (select(sql_money1, param))
                print("转入账户的余额", num_w1)
                return 0


# 查询
def inquire():
    inquire_username = int(input("请输入要查询的账户:"))
    sql_2 = "SELECT account FROM USER where account =%s"
    param = [inquire_username]
    data1 = len(select(sql_2, param))
    if data1 == 0:
        print("该用户名不存在")
    else:
        inquire_password = int(input("请输入账户密码:"))
        sql_password = "SELECT account FROM USER WHERE PASSWORD =%s and account=%s"
        param = [inquire_password, inquire_username]
        select(sql_password, param)
        password_num = len(select(sql_password, param))
        if password_num == 0:
            print("密码不正确")
        else:
            sql_1 = "SELECT * FROM USER WHERE account = %s"
            param = [inquire_username]
            num = select(sql_1, param)
            print(num)


while True:
    o = input("请选择业务:")
    if o == "1":
        print("1、开户")
        adduser()
    elif o == "2":
        print("2、存钱")
        login()
    elif o == "3":
        print("3、取钱")
        cancel()
    elif o == "4":
        print("4、转账")
        turn_money_num()
    elif o == "5":
        inquire()
    elif o == "6":
        print("再见")
        break
